home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / att.com / ATT.C < prev   
Encoding:
C/C++ Source or Header  |  1989-05-04  |  6.8 KB  |  180 lines

  1. /*
  2.         Att v1.0 ■ (c) 1989 Tony Tortorelli
  3.  
  4.             Usage:
  5.                    Att [d:][path][+-hsra /d] filename
  6.  
  7.                         h Hidden     s System
  8.                         r Read Only  a Archive
  9.                        /d Include directories")
  10. */
  11.  
  12. #include <stdio.h>
  13. #include <errno.h>
  14. #include <conio.h>
  15. #include <string.h>
  16. #include <process.h>
  17. #include <dir.h>
  18. #include <dos.h>
  19. #include <io.h>
  20.  
  21. void help(void);
  22.  
  23. void main(int argc, char *argv[])
  24. {
  25.     char c,directory='n',change='n';
  26.     char dir[MAXDIR],path[MAXPATH],file[MAXPATH];
  27.     char oldatt[5],newatt[5];
  28.     int i,flag=-1,OldAttribute,NewAttribute,set=0,reset=0;
  29.     struct ffblk ffblk;
  30.  
  31.     directvideo=0;     /* Use BIOS */
  32.     if(_osmajor < 2){
  33.         fputs("\a\n\t*** Requires DOS 2.0 or greater ***",stderr);
  34.         exit(0);
  35.     }
  36.     if(argc<2)
  37.         help();
  38.     else{
  39.         file[0]=NULL;
  40.         while(--argc>0){
  41.             ++argv;
  42.             if((c=**argv)=='+'){  /* Set Flag */
  43.                 change='y';
  44.                 while((c=(*(++(*argv))))!=NULL){
  45.                     switch(c){
  46.                         case 'h':
  47.                         case 'H':
  48.                             set |= FA_HIDDEN;
  49.                             break;
  50.                         case 's':
  51.                         case 'S':
  52.                             set |= FA_SYSTEM;
  53.                             break;
  54.                         case 'r':
  55.                         case 'R':
  56.                             set |= FA_RDONLY;
  57.                             break;
  58.                         case 'a':
  59.                         case 'A':
  60.                             set |= FA_ARCH;
  61.                             break;
  62.                         }
  63.                     }
  64.                 }
  65.             else if(c=='-'){      /* Reset Flag */
  66.                 change='y';
  67.                 while((c=(*(++(*argv))))!=NULL){
  68.                     switch(c){
  69.                         case 'h':
  70.                         case 'H':
  71.                             reset |= FA_HIDDEN;
  72.                             break;
  73.                         case 's':
  74.                         case 'S':
  75.                             reset |= FA_SYSTEM;
  76.                             break;
  77.                         case 'r':
  78.                         case 'R':
  79.                             reset |= FA_RDONLY;
  80.                             break;
  81.                         case 'a':
  82.                         case 'A':
  83.                             reset |= FA_ARCH;
  84.                             break;
  85.                         }
  86.                     }
  87.                 }
  88.             else if(c=='/'){      /* Directory option and help */
  89.                 while((c=(*(++(*argv))))!=NULL){
  90.                     switch(c){
  91.                         case '?':
  92.                             help();
  93.                             break;
  94.                         case 'd':
  95.                         case 'D':
  96.                                 directory='y';
  97.                             break;
  98.                         default:
  99.                             help();
  100.                             break;
  101.                         }
  102.                     }
  103.                 }                 /* Find first file, or quit */
  104.             else{
  105.                 if((flag=findfirst(*argv,&ffblk,FA_SYSTEM | FA_HIDDEN | FA_RDONLY | FA_ARCH | FA_DIREC)) == -1){
  106.                     fprintf(stderr,"\"%s\" not found",strlwr(*argv));
  107.                     exit(0);
  108.                 }
  109.                 fnsplit(*argv,dir,path,file,NULL);
  110.             }
  111.         }   /* End of while */
  112.     }     /* End of else */
  113.     if(file[0] == NULL)          /* If no file entered, quit */
  114.         help();
  115.     if(change == 'y'){           /* heading for changes */
  116.         cputs("\r\nFilename         Old    New");
  117.         cputs("\r\n────────────    ─────  ─────\r\n");
  118.     }
  119.     else{                        /* heading for viewing only */
  120.         cputs("\r\nFilename         Att");
  121.         cputs("\r\n────────────    ─────\r\n");
  122.     }
  123.     while(!flag){
  124.         if(ffblk.ff_name[0] == '.' || directory == 'n' && (ffblk.ff_attrib & FA_DIREC)){
  125.             flag=findnext(&ffblk);    /* Skip '.', and directory (if /d not set) */
  126.             continue;
  127.         }
  128.         fnmerge(file,dir,path,ffblk.ff_name,NULL);
  129.         OldAttribute = _chmod(file,0); /* Find old attribute */
  130.         NewAttribute = OldAttribute;   /* copy to new attribute */
  131.         NewAttribute |= set;           /* OR 'set' flags */
  132.         NewAttribute &= ~reset;        /* AND reset flags */
  133.         i=0;                           /* store old attributes */
  134.         (ffblk.ff_attrib & FA_SYSTEM)  ? (oldatt[i++] = 's') : (oldatt[i++] = '─');
  135.         (ffblk.ff_attrib & FA_HIDDEN)  ? (oldatt[i++] = 'h') : (oldatt[i++] = '─');
  136.         (ffblk.ff_attrib & FA_RDONLY)  ? (oldatt[i++] = 'r') : (oldatt[i++] = '─');
  137.         (ffblk.ff_attrib & FA_ARCH)    ? (oldatt[i++] = 'a') : (oldatt[i++] = '─');
  138.         (ffblk.ff_attrib & FA_DIREC)   ? (oldatt[i++] = 'd') : (oldatt[i++] = '─');
  139.         oldatt[i] = NULL;
  140.         if(change == 'y'){             /* only enter loop if set or reset requested */
  141.             i=0;                       /* store old attributes */
  142.             (NewAttribute & FA_SYSTEM)  ? (newatt[i++] = 's') : (newatt[i++] = '─');
  143.             (NewAttribute & FA_HIDDEN)  ? (newatt[i++] = 'h') : (newatt[i++] = '─');
  144.             (NewAttribute & FA_RDONLY)  ? (newatt[i++] = 'r') : (newatt[i++] = '─');
  145.             (NewAttribute & FA_ARCH)    ? (newatt[i++] = 'a') : (newatt[i++] = '─');
  146.             (NewAttribute & FA_DIREC)   ? (newatt[i++] = 'd') : (newatt[i++] = '─');
  147.             newatt[i] = NULL;
  148.             if(NewAttribute != OldAttribute){  /* change only if new attribute is different */
  149.                 if((_chmod(file,1,(NewAttribute & ~FA_DIREC))) == -1){  /* don't attempt to change directory flag */
  150.                     fprintf(stderr,"%-16s",strlwr(ffblk.ff_name));      
  151.                     printf(strerror(errno));
  152.                     flag=findnext(&ffblk);
  153.                     continue;
  154.                 }
  155.             }
  156.         }
  157.         if(change == 'y')
  158.             cprintf("%-12s    %-4s  %-4s",strlwr(ffblk.ff_name),oldatt,newatt);
  159.         else
  160.             cprintf("%-12s    %-4s",strlwr(ffblk.ff_name),oldatt);
  161.         if(NewAttribute == OldAttribute && change == 'y')
  162.             cputs("  Unchanged\r\n");
  163.         else
  164.             cputs("\r\n");
  165.         flag=findnext(&ffblk);
  166.     }
  167. }
  168.  
  169. void help(void)
  170. {
  171.     cputs("Att v1.0 ■ (c) 1989 Tony Tortorelli\r\n"
  172.           "\r\n    Usage:"
  173.           "\r\n           Att [d:][path][+-hsra /d] filename"
  174.           "\r\n"
  175.           "\r\n               h Hidden     s System"
  176.           "\r\n               r Read Only  a Archive"
  177.           "\r\n              /d Include directories\r\n");
  178.     exit(0);
  179. }
  180.